﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Entities;
using Totoro.ORMapping;
using NHibernate;

namespace Bussiness.ModelQuery
{
    public class UserManager
    {
        public UserManager()
        {

        }
        private static UserManager instance;
        public static UserManager Instacne
        {
            get
            {
                if (instance == null)
                {
                    instance = new UserManager();
                    instance.LoadData();
                }
                return instance;
            }
        }
        Dictionary<string, User> userCaches = new Dictionary<string, User>();
        public void LoadData()
        {
            userCaches.Clear();
            ISession session = NHHelper.GetCurrentSession();
            ICriteria crt = session.CreateCriteria<Employee>();
            IList<User> userList = null;
            userList = crt.List<User>();
            if (userList != null)
            {
                foreach (User user in userList)
                {
                    userCaches.Add(user.ID, user);
                }
            }
        }
        /// <summary>
        /// 根据Id获取用户
        /// </summary>
        /// <param name="sId">Id</param>
        /// <returns></returns>
        public User GetUserById(string sId)
        {
            if (userCaches.ContainsKey(sId))
            {
                return userCaches[sId];
            }
            return null;
        }
        /// <summary>
        /// 获取所有的用户
        /// </summary>
        /// <returns></returns>
        public User[] GetUsers()
        {
            return userCaches.Values.ToArray();
        }
        /// <summary>
        /// 登录是否成功
        /// </summary>
        /// <param name="sLogionName">登录名</param>
        /// <param name="sPassword">登录密码（密码应该设计加密机制（待处理））</param>
        /// <returns></returns>
        public bool LogionCheck(string sLogionName, string sPassword)
        {
            if (userCaches.Values == null || userCaches.Values.Count == 0)
                return false;
            foreach (User user in userCaches.Values)
            {
                if (user.LogonName == sLogionName && user.PassWord == sPassword)
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sId"></param>
        public void DeleteUserByID(string sId)
        {
            ISession session = NHHelper.GetCurrentSession();
            User user = GetUserById(sId);
            if (user != null)
            {
                session.Delete(user);
                userCaches.Remove(sId);
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="productComment"></param>
        public void InsertUserComment(User user)
        {
            NHHelper.SaveOjb(user);
            userCaches.Add(user.ID, user);
        }
    }
}
